﻿using System;
using System.Globalization;
using System.Reflection;
using System.Windows.Data;
using MediaMover.Common.TV;
using MediaMover.MVVM.ViewModels;

namespace MediaMover.MVVM.Models
{
    public class OutgoingFileNameConverter : IMultiValueConverter
    {
        public ScanViewModel ScanViewModel { private get; set; }
        public Episode Episode { private get; set; }
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (ScanViewModel == null)
                ScanViewModel = (ScanViewModel) values[0];
            if (Episode == null)
                Episode = (Episode) values[1];
            string fileName = ScanViewModel.EpisodeNameFormat;
            PropertyInfo[] propertyInfos = typeof (Episode).GetProperties();
            foreach (var propertyInfo in propertyInfos)
            {
                fileName = fileName.Replace("{" + propertyInfo.Name + "}", propertyInfo.GetValue(Episode,null).ToString());
            }
            propertyInfos = typeof (Season).GetProperties();
            foreach (var propertyInfo in propertyInfos)
            {
                fileName = fileName.Replace("{" + propertyInfo.Name + "}", propertyInfo.GetValue(Episode.Season, null).ToString());
            }
            propertyInfos = typeof (Show).GetProperties();
            foreach (var propertyInfo in propertyInfos)
            {
                fileName = fileName.Replace("{" + propertyInfo.Name + "}", propertyInfo.GetValue(Episode.Show, null).ToString());
            }
            return fileName;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
